﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WCSFContrib.ECommerceCatalog.BusinessEntities;
using WCSFContrib.ECommerceCatalog.Product.Views;
using WCSFContrib.ECommerceCatalog.Product.Tests.Mocks;

namespace WCSFContrib.ECommerceCatalog.Product.Tests
{
    /// <summary>
    /// Summary description for DefaultPresenterTestFixture
    /// </summary>
    [TestClass]
    public class DefaultPresenterTestFixture
    {
        private MockDefaultView _view;
        private MockProductController _controller;

        public DefaultPresenterTestFixture()
        {
        }

        [TestMethod]
        public void ShouldGetAProductAndShowItsDetails()
        {
            DefaultPresenter presenter = GetDefaultPresenterInitialized();
            
            presenter.OnProductDetailsRequested(0);

            Assert.IsTrue(_controller.GetProductCalled);
            Assert.IsTrue(_view.ShowProductDetailsCalled);
        }

        [TestMethod]
        public void ShouldSelectProductsWhenSearchTextIsNullOrEmpty()
        {
            DefaultPresenter presenter = GetDefaultPresenterInitialized();

            presenter.OnProductsSelecting("");

            Assert.IsFalse(_controller.SearchProductsCalled);
            Assert.IsTrue(_view.ShowProductsCalled);
            Assert.AreEqual(_view.TotalProductsCount, 0);
        }

        [TestMethod]
        public void ShouldSelectProductsWhenSearchTextIsNotNullAndIsNotEmpty()
        {
            DefaultPresenter presenter = GetDefaultPresenterInitialized();

            presenter.OnProductsSelecting("product");

            Assert.IsTrue(_controller.SearchProductsCalled);
            Assert.IsTrue(_view.ShowProductsCalled);
            Assert.AreNotEqual(_view.TotalProductsCount, 0);
        }

        [TestMethod]
        public void ShouldShowCurrentPageWhenTheSearchTextChanged()
        {
            DefaultPresenter presenter = GetDefaultPresenterInitialized();

            presenter.OnSearchTextChanged();

            Assert.IsTrue(_view.ShowCurrentPageCalled);
        }

        [TestMethod]
        public void ShouldAddProductToCart()
        {
            DefaultPresenter presenter = GetDefaultPresenterInitialized();

            presenter.OnAddProductToCart("", 0, 0);

            Assert.IsTrue(_controller.AddProductToCartCalled);
            Assert.IsTrue(_controller.GoCartPageCalled);
        }

        private DefaultPresenter GetDefaultPresenterInitialized()
        {
            _controller = new MockProductController();
            _view = new MockDefaultView();
            DefaultPresenter presenter = new DefaultPresenter(_controller);
            presenter.View = _view;

            return presenter;
        }
    }

    class MockDefaultView : IDefaultView
    {
        public bool ShowProductDetailsCalled = false;
        public bool TotalProductsCountCalled = false;
        public bool ShowProductsCalled = false;
        public bool ShowCurrentPageCalled = false;

        private int _totalProducts = 0;

        #region IDefaultView Members

        public void ShowProducts(ICollection<Item> items)
        {
            ShowProductsCalled = true;
        }

        public void HidePagesPanel()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public void ShowSearchText(string searchText)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public void ShowPagesCounters()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public void ShowProductDetails(Item item)
        {
            ShowProductDetailsCalled = true;
        }

        public void ShowCurrentPage(int currentPage)
        {
            ShowCurrentPageCalled = true;
        }

        public int TotalProductsCount
        {
            get { return _totalProducts; }
            set
            {
                _totalProducts = value;
                TotalProductsCountCalled = true;
            }
        }

        public int MaximumRowsPerPage
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        public int PageCount
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        #endregion
    }
}

